package cz.incad.psh.oai_solr;

import java.util.Arrays;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.namespace.NamespaceContext;

/**
 *
 * @author Alberto Hernandez
 */
public class OAINamespaceContext implements NamespaceContext {

    private static final Map<String, String> MAP_PREFIX2URI = new IdentityHashMap<String, String>();
    private static final Map<String, String> MAP_URI2PREFIX = new IdentityHashMap<String, String>();

    static {
        MAP_PREFIX2URI.put("dc", "http://purl.org/dc/elements/1.1/");
        MAP_PREFIX2URI.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
        MAP_PREFIX2URI.put("oai", "http://www.openarchives.org/OAI/2.0/");
        MAP_PREFIX2URI.put("marc", "http://www.loc.gov/MARC21/slim");

        for (Map.Entry<String, String> entry : MAP_PREFIX2URI.entrySet()) {
            MAP_URI2PREFIX.put(entry.getValue(), entry.getKey());
        }
    }

    @Override
    public String getNamespaceURI(String arg0) {
        return MAP_PREFIX2URI.get(arg0.intern());
    }

    @Override
    public String getPrefix(String arg0) {
        return MAP_URI2PREFIX.get(arg0.intern());
    }

    @Override
    public Iterator getPrefixes(String arg0) {
        String prefixInternal = MAP_URI2PREFIX.get(arg0.intern());
        if (prefixInternal != null) {
            return Arrays.asList(prefixInternal).iterator();
        } else {
            return Collections.emptyList().iterator();
        }
    }
    
}
